using System.Collections.Generic;

public class FSM<T> where T : IFSMState
{
    public Dictionary<System.Type, T> StateTable{ get; protected set; } //状态表
    public T PrevState{ get; protected set; } //前一个状态
    protected T curState; //当前状态
    public FSM()
    {
        StateTable = new Dictionary<System.Type, T>();
        curState = PrevState = default;
    }
    public void AddState(T state)
    {
        StateTable.Add(state.GetType(), state);
    }
    public void SwitchOn(T startState)
    {
        curState = startState;
        curState.Enter();
    }
    public void SwitchOn(System.Type startState)
    {
        curState = StateTable[startState];
        curState.Enter();
    }
    public void ChangeState(T nextState)
    {
        PrevState = curState;
        curState.Exit();
        curState = nextState;
        curState.Enter();
    }
    public void ChangeState(System.Type nextState)
    {
        PrevState = curState;
        curState.Exit();
        curState = StateTable[nextState];
        curState.Enter();
    }
    public void RevertToPrevState()
    {
        if(PrevState != null) 
        {
            ChangeState(PrevState);
        }
    }
    public void OnUpdate()
    {
        curState.LogicalUpdate();
    }
}

